package com.breakfast.fun.ui.user.address;

import android.content.Intent;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.breakfast.fun.R;
import com.breakfast.fun.base.BaseActivity;
import com.breakfast.fun.data.entity.user.address.AddressData;
import com.breakfast.fun.data.entity.user.address.BuildingData;
import com.breakfast.fun.event.RefreshEvent;
import com.breakfast.fun.util.L;
import com.breakfast.fun.util.PopUtil;
import com.breakfast.fun.util.T;
import com.breakfast.fun.view.widget.TitleBarView;
import com.breakfast.fun.view.widget.WheelPicker;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;

public class AddAddressActivity extends BaseActivity<AddAddressPresenter, AddAddressModel> implements AddAddressContract.View {

    @Bind(R.id.tbv_add_address)
    TitleBarView mTbvAddAddress;
    @Bind(R.id.et_add_address_name)
    EditText mEtAddAddressName;
    @Bind(R.id.et_add_address_mobile)
    EditText mEtAddAddressMobile;
    @Bind(R.id.tv_add_address_building)
    TextView mTvAddAddressBuilding;
    @Bind(R.id.tv_add_address_floor)
    TextView mTvAddAddressFloor;
    @Bind(R.id.et_add_address_detail)
    EditText mEtAddAddressDetail;
    @Bind(R.id.btn_add_address)
    Button mBtnAddAddress;
    @Bind(R.id.ll_add_address)
    LinearLayout mLlAddAddress;

    private BuildingData mBuildingData;

    @Override
    public int getLayoutId() {
        return R.layout.activity_add_address;
    }

    @Override
    public void initView() {

        EventBus.getDefault().register(this);
        mTbvAddAddress.setTitleNoRight("添加地址");
        mTbvAddAddress.setBack(this);

        mTvAddAddressFloor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopPicker();
            }
        });

        mTvAddAddressBuilding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AddAddressActivity.this, ChooseBuildingActivity.class);
                startActivity(intent);
            }
        });

        mBtnAddAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mEtAddAddressMobile.getText().toString())
                        || mEtAddAddressMobile.getText().toString().length() != 11) {
                    T.show("请检查手机号格式");
                    return;
                }
                if (TextUtils.isEmpty(mEtAddAddressName.getText().toString())) {
                    T.show("姓名不能为空");
                    return;
                }
                if (mTvAddAddressBuilding.getText().toString().equals("请选择送餐大楼")) {
                    T.show("请选择送餐大楼");
                    return;
                }
                if (mTvAddAddressFloor.getText().toString().equals("请选择公司楼层")) {
                    T.show("请选择公司楼层");
                    return;
                }
                //详细地址拒绝有：后面，旁边，对面，附近等信息，如果疑问请联系客服：4006059144
                if (mEtAddAddressDetail.getText().toString().contains("后面") || mEtAddAddressDetail.getText().toString().contains("旁边") || mEtAddAddressDetail.getText().toString().contains("对面") || mEtAddAddressDetail.getText().toString().contains("附近")) {
                    T.show("请不要使用后面，旁边，对面，附近等信息");
                    return;
                }
                AddressData addressData = new AddressData();
                addressData.setConsignee(mEtAddAddressName.getText().toString());
                addressData.setPhone(mEtAddAddressMobile.getText().toString());
                if (mBuildingData != null) {
                    addressData.setBuilderId(mBuildingData.getId());
                    addressData.setBuilderName(mBuildingData.getBuilderName());
                }
                addressData.setFloorNo(mTvAddAddressFloor.getText().toString());
                addressData.setDetail(mEtAddAddressDetail.getText().toString());
                presenter.addAddress(addressData);
            }
        });

    }

    private void showPopPicker() {
        //popwindow要装载的view
        View contentView = LayoutInflater.from(context).inflate(R.layout.widget_pop_picker, null);

        TextView tvWheelpickerCancel = (TextView) contentView.findViewById(R.id.tv_wheelpicker_cancel);
        TextView tvWheelpickerOk = (TextView) contentView.findViewById(R.id.tv_wheelpicker_ok);
        final WheelPicker wpWheelPicker = (WheelPicker) contentView.findViewById(R.id.wp_wheelpicker);

        final PopupWindow popupWindow = new PopupWindow(contentView, WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT);
        PopUtil.setPop(popupWindow);

        popupWindow.showAtLocation(AddAddressActivity.this.findViewById(R.id.ll_add_address), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        backgroundAlpha(0.5f);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });


        tvWheelpickerOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String floor = (String) wpWheelPicker.getData().get(wpWheelPicker.getCurrentItemPosition());
                mTvAddAddressFloor.setText(floor);
                popupWindow.dismiss();
            }
        });

        tvWheelpickerCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
    }

    @Override
    public void showSuccess() {
        finish();
        EventBus.getDefault().post(new RefreshEvent("UserAddressActivity"));
    }

    @Override
    public void setBuilding(BuildingData buildingData) {
        this.mBuildingData = buildingData;
        mTvAddAddressBuilding.setText(buildingData.getBuilderName());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBuilding(BuildingData data) {
        presenter.chooseBuilding(data);
    }

    @Override
    protected void destroyView() {
        super.destroyView();
        EventBus.getDefault().unregister(this);
    }
}
